package com.hissy.common.generator_01;

import java.io.IOException;
import java.io.OutputStream;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * Created by zhangyanfei on 2018/5/29.
 *
 * @author zhangyanfei
 * @date 2018/05/29
 */
public interface Serializer<T> {

    /**
     * 将对象转换为xml
     *
     * @param instance
     * @param out
     * @param isRoot
     */
    void toXml(T instance, OutputStream out, boolean isRoot);

    /**
     * 将对象转换为JSON
     *
     * @param instance
     * @param out
     * @param isRoot
     */
    void toJson(T instance, OutputStream out, boolean isRoot);

    /**
     * Api 序列化的特征
     */
    class ApiSerializerFeature {
        /**
         * 使用fastJSON作为json序列化实现，序列化的特性定义
         */
        public static final SerializerFeature[] SERIALIZER_FEATURES = new SerializerFeature[] {
            //不允许循环引用
            SerializerFeature.DisableCircularReferenceDetect,
            //null属性，序列化为null
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.NotWriteRootClassName,
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteDateUseDateFormat
        };
    }

    /**
     * jsonString的序列化器
     */
    Serializer<JSONString> jsonStringSerializer = new Serializer<JSONString>() {
        @Override
        public void toXml(JSONString instance, OutputStream out, boolean isRoot) {
            try {
                if (instance.value != null) {
                    out.write(instance.value.getBytes(ConstField.UTF8));
                }
            } catch (Exception e) {
                //todo
            }

        }

        @Override
        public void toJson(JSONString instance, OutputStream out, boolean isRoot) {
            try {
                if (instance.value != null) {
                    out.write(instance.value.getBytes(ConstField.UTF8));
                }
            } catch (Exception e) {
                //todo
            }
        }
    };

    /**
     * RawString的序列化器
     */
    Serializer<RawString> rawStringSerializer = new Serializer<RawString>() {
        @Override
        public void toXml(RawString instance, OutputStream out, boolean isRoot) {
            try {
                if (instance.value != null) {
                    out.write(instance.value.getBytes(ConstField.UTF8));
                }
            } catch (Exception e) {
                //todo
            }

        }

        @Override
        public void toJson(RawString instance, OutputStream out, boolean isRoot) {
            try {
                if (instance.value != null) {
                    out.write(instance.value.getBytes(ConstField.UTF8));
                }
            } catch (Exception e) {
                //todo
            }
        }
    };

    /**
     * 对象数组序列化
     */
    Serializer<ObjectArrayResp> objectArrayRespSerializer = new Serializer<ObjectArrayResp>() {
        byte[][] bs = new byte[8][];

        //转成xml的格式
        {
            bs[0] = "<ObjectArrayResp>".getBytes(ConstField.UTF8);
            bs[1] = "<value>".getBytes(ConstField.UTF8);
            bs[2] = "<item>".getBytes(ConstField.UTF8);
            bs[3] = "</item>".getBytes(ConstField.UTF8);
            bs[4] = "</value>".getBytes(ConstField.UTF8);
            bs[5] = "</ObjectArrayResp>".getBytes(ConstField.UTF8);
            bs[6] = "<![CDATA[".getBytes(ConstField.UTF8);
            bs[7] = "]]>".getBytes(ConstField.UTF8);
        }

        @Override
        public void toXml(ObjectArrayResp instance, OutputStream out, boolean isRoot) {
            if (instance == null) {
                return;
            }
            try {
                if (isRoot) {
                    out.write(bs[0]);
                }
                if (isRoot) {
                    out.write(bs[0]);
                }
                if (instance.value != null) {
                    out.write(bs[1]);
                    for (Object obj : instance.value) {
                        out.write(bs[2]);
                        if (obj != null) {
                            if (obj.getClass() == String.class) {
                                out.write(bs[6]);
                                out.write(obj.toString().getBytes(ConstField.UTF8));
                                out.write(bs[7]);
                            } else if (obj.getClass().isEnum()) {
                                out.write(bs[6]);
                                out.write(((Enum)obj).name().getBytes(ConstField.UTF8));
                                out.write(bs[7]);
                            } else {
                                /**
                                 * 将对象转换为xml
                                 */
                                //Serializer localSerializer = POJOSerializerProvider.getSerializer(obj.getClass());
                                //localSerializer.toXml(obj, out, false);
                            }
                        }
                        out.write(bs[3]);
                    }
                    out.write(bs[4]);
                }
                if (isRoot) {
                    out.write(bs[5]);
                }
            } catch (Exception e) {
                //todo
            }
        }

        @Override
        public void toJson(ObjectArrayResp instance, OutputStream out, boolean isRoot) {
            try {
                out.write(JSON.toJSONBytes(instance, ApiSerializerFeature.SERIALIZER_FEATURES));
            } catch (IOException localIOException) {
                //todo
            }
        }
    };
}
